

package com.controller;


import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import com.model.User;
import com.service.UserService;
import java.util.List;

public class UserController extends HttpServlet {
   
   
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

          try {
            UserService userService = new UserService();
            userService.setConnection();
            List<User> list = userService.getAll();
            userService.closeConnection();
            request.setAttribute("viewProfile", list);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/viewProfile.jsp");
            dispatcher.forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

         
          try{

            String firstName = request.getParameter("firstName");
            String lastName = request.getParameter("lastName");
            String age1 = request.getParameter("age");
            String userName = request.getParameter("userName");
            String password = request.getParameter("password");

            boolean error = false;

            if (firstName.length() == 0 || firstName == null) {
                error = true;
            }

            if ( lastName.length()  == 0 ||  lastName == null) {
                error = true;
            }

            if (  age1.length() == 0 ||  age1 == null) {
                error = true;
            }

            if ( userName.length() == 0 || userName == null) {
                error = true;
            }
             if ( password.length()  == 0 || password == null) {
                error = true;
            }

            if (error) {
                request.setAttribute("errorMsg", "All Fields are required !");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/register.jsp");
                requestDispatcher.forward(request, response);
                
            }
            else{

                request.setAttribute("successMsg", "Congrats! You have registered Successfully");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/register.jsp");
                requestDispatcher.forward(request, response);
                
                
            }


            int age = Integer.parseInt(age1);
            
            User user = new User();
            user.setFirstName(firstName);
            user.setLastName(lastName);
            user.setAge(age);
            user.setUserName(userName);
            user.setPassword(password);
            user.setActive(false);
            

            UserService userService = new UserService();
        
            userService.setConnection();
            userService.create(user);

            
            userService.closeConnection();
            
            
        }
            catch (Exception ex) {
            ex.printStackTrace();
       
            

          }

    
    }

}
